package com.yunji.storage.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yunji.common.annotation.EventRecord;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.constant.EventBusinessTypeConstant;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.storage.bo.StockTakeRemoveBO;
import com.yunji.storage.bo.StockTakeResetBO;
import com.yunji.storage.bo.StockTakeSelectQueryBO;
import com.yunji.storage.bo.StorageUpdateAuditBO;
import com.yunji.storage.constant.StorageEventTypeConstant;
import com.yunji.storage.dao.IStockTakeDao;
import com.yunji.storage.domain.StockTakeDO;
import com.yunji.storage.enums.StockTakeStatus;
import com.yunji.storage.mapper.StockTakeMapper;
import org.springframework.stereotype.Repository;

import java.util.Arrays;

@Repository
public class StockTakeDaoImpl extends BaseDaoImpl<StockTakeMapper, StockTakeDO> implements IStockTakeDao {
    @Override
    public StockTakeDO select(StockTakeSelectQueryBO bo) {
        return getOne(new LambdaQueryWrapper<StockTakeDO>()
                .eq(BeanUtils.isNotNull(bo.getBelongId()), StockTakeDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getStocktakeStatus()), StockTakeDO::getStocktakeStatus, bo.getStocktakeStatus())
                .eq(BeanUtils.isNotNull(bo.getStocktakeCode()), StockTakeDO::getStocktakeCode, bo.getStocktakeCode()));
    }

    @Override
    public boolean deleteByTicket(StockTakeRemoveBO bo) {
        return remove(new LambdaQueryWrapper<StockTakeDO>()
                .eq(BeanUtils.isNotNull(bo.getBelongId()), StockTakeDO::getBelongId, bo.getBelongId())
                .eq(BeanUtils.isNotNull(bo.getStocktakeStatus()), StockTakeDO::getStocktakeStatus, bo.getStocktakeStatus()));
    }

    @Override
    public boolean delete(Long... stockTakeIds) {
        return remove(new LambdaQueryWrapper<StockTakeDO>()
                .in(StockTakeDO::getStocktakeId, Arrays.asList(stockTakeIds)));
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_STOCKTAKE,eventType = StorageEventTypeConstant.EVENT_TYPE_STORAGE)
    public boolean create(StockTakeDO stockTake) {
        return save(stockTake);
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_STOCKTAKE,eventType = StorageEventTypeConstant.EVENT_TYPE_STORAGE)
    public boolean updateTicket(StockTakeDO update) {
        return updateById(update);
    }

    @Override
    public void reset(StockTakeResetBO bo) {
        baseMapper.reset(bo);
    }

    @Override
    @EventRecord(businessType = EventBusinessTypeConstant.StorageType.EVENT_STORAGE_STOCKTAKE,eventType = StorageEventTypeConstant.EVENT_TYPE_AUDIT)
    public boolean updateAuditStatus(StorageUpdateAuditBO auditBO) {
        return update(new LambdaUpdateWrapper<StockTakeDO>()
                .set(BeanUtils.isNotNull(auditBO.getAuditStatus()),StockTakeDO::getAuditStatus,auditBO.getAuditStatus())
                .set(BeanUtils.isNotNull(auditBO.getAuditId()),StockTakeDO::getAuditId,auditBO.getAuditId())
                .set(BeanUtils.isNotNull(auditBO.getAuditName()),StockTakeDO::getAuditName,auditBO.getAuditName())
                .set(BeanUtils.isNotNull(auditBO.getAuditBelongId()),StockTakeDO::getAuditBelongId,auditBO.getAuditBelongId())
                .set(BeanUtils.isNotNull(auditBO.getAuditBelongName()),StockTakeDO::getAuditBelongName,auditBO.getAuditBelongName())
                .set(BeanUtils.isNotNull(auditBO.getAuditTime()),StockTakeDO::getAuditTime,auditBO.getAuditTime())
                .eq(StockTakeDO::getStocktakeId,auditBO.getBusinessId()));
    }
}
